<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }

Class ConfigController Extends baseController {

	private $numOfRow = 5;

	function __construct($registry) {
		$this->registry = $registry;
		$this->numOfRow = $this->registry->db->get('configManager')->getByKey('SettingTableSize')->get('value');
	}

	public function index($user, $page = null)
	{
		$tags = array('bstoContentTitle' => "View Configuration");
		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}
				
		$content = <<<EOT
Welcome to Setting Control Panel!
EOT;

		$path = BSTO_ROOT_PATH . "config";
		 
		$totalConfig = $this->registry->db->get('configManager')->countConfigById();
		//echo $totalConfig;
		
		$numPage = ceil($totalConfig/$this->numOfRow);		

		$fst = $this->numOfRow * ($page - 1);
			
		$pageLeft = $page - 1;
		if ($pageLeft <= 0){
			$pageLeft = 1;
		}
		$pageLeft = $path. "/view/". $pageLeft . "?curRow=" . $this->numOfRow;
		$pageFarLeft = 1;
		$pageFarLeft = $path. "/view/". $pageFarLeft . "?curRow=" . $this->numOfRow;
		$pageRight = $page + 1;
		if ($pageRight > $numPage){
			$pageRight = $numPage;
		}
		$pageRight = $path. "/view/". $pageRight . "?curRow=" . $this->numOfRow;
		$pageFarRight = $numPage;		
		$pageFarRight = $path. "/view/". $pageFarRight . "?curRow=" . $this->numOfRow;
	  
		$tmpContent = $this->registry->template->getModule('vOneConfig', $tags);		

		$strContent ="";
		$strContentTotal = '';


		$arrResult = $this->registry->db->get('configManager')->getConfigLimit($fst, $this->numOfRow);

		$id = 0;
		foreach ($arrResult as $ele){
			if( $id%2 == 0 ) {
				$strContent = str_replace('{class}', '', $tmpContent);
			} else {
				$strContent = str_replace('{class}', 'alternate-row', $tmpContent);
			}
			$strContent = str_replace('{BstoId}', $ele['id'], $strContent);
			$strContent = str_replace('{BstoKey}', $ele['key'], $strContent);
			$strContent = str_replace('{BstoValue}', $ele['value'], $strContent);
			$strContent = str_replace('{BstoPage}', $page, $strContent);
			$strContentTotal = $strContentTotal . $strContent;
			$id++;
		}
			
		$strContentTotal = str_replace('{path}', $path, $strContentTotal);

		$tmp2Content = $this->registry->template->getModule('vconfig', $tags);
		$content = '';
		$content = str_replace('{BstoContent}', $strContentTotal, $tmp2Content);
		$content = str_replace('{page}', $page, $content);
		$content = str_replace('{pageLeft}', $pageLeft, $content);
		$content = str_replace('{pageFarLeft}', $pageFarLeft, $content);
		$content = str_replace('{pageRight}', $pageRight, $content);
		$content = str_replace('{pageFarRight}', $pageFarRight, $content);
		$content = str_replace('{numPage}', $numPage, $content);
		
		$this->registry->template->setMenuKey('setting');
		$output = $this->registry->template->getPageContent('index', 'BSTO ACP - Setting Management', $content, $tags);

		echo $output;
	}

	/*
	public function delete($id)
	{
		$this->registry->db->get('configManager')->setDelConfigById($id);
		//$this->index();
		
	}
	*/
	
	public function edit($user, $page, $id)
	{
		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}
				
		$method = $_SERVER['REQUEST_METHOD'];
		if ($method == 'POST'){
			$value = strip_tags($_POST["value"]);
			$this->registry->db->get('configManager')->updateConfig($id, $value);

			//come to last page
			$this->linkToPreviousPage($page);	
		} else {
			$tags = array('bstoContentTitle' => "Edit Configuration");
			$content = $this->registry->template->getModule('eConfig', $tags);

			$arrResult =  $this->registry->db->get('configManager')->getConfigById($id);
			$content = str_replace('{key}', $arrResult['key'], $content);
			$content = str_replace('{value}', $arrResult['value'], $content);

			$this->registry->template->setMenuKey('config');
			$output = $this->registry->template->getPageContent('index', 'BSO ACP - Edit Setting', $content, $tags);

			echo $output;
		}
	}

/*	public function add($user, $id)
	{
		if(isset($_GET['numRows'])){
			$this->numOfRow = $_GET['numRows'];
		}else if(isset($_GET['curRow'])){
			$this->numOfRow = $_GET['curRow'];
		}
		
		$method = $_SERVER['REQUEST_METHOD'];
		if ($method == 'POST'){
			$key = strip_tags($_POST["key"]);
			$value = strip_tags($_POST["myValue"]);
			$this->registry->db->get('configManager')->addConfig($key, $value);
						
			$this->linkToLastPage();
		} else {
			$tags = array('bstoContentTitle' => "Add Configuration");
			$content = $this->registry->template->getModule('aconfig', $tags);
			$output = $this->registry->template->getPageContent('index', 'setting', $content, $tags);

			echo $output;
		}
	}*/

	public function linkToLastPage()
	{
		$totalStatic = $this->registry->db->get('configManager')->countConfigById();
		$page = ceil($totalStatic/$this->numOfRow);
		header("Location: ../view/$page");
		exit();
	}
	
	public function linkToPreviousPage($page) 
	{
		$total = $this->registry->db->get('configManager')->countConfigById();
		$page = ($page <= ceil($total/$this->numOfRow)) ? $page : $total/$this->numOfRow;
		header("Location: ../../view/$page");
		exit();		
	}
}
?>
